package cn.jingyuan.bee.paging;

import java.io.Serializable;

/**
 * 分页查询模型
 */
public class PageQueryBean<Q extends Serializable> implements Serializable {

    /** 当前页数 */
    private int page = 1;

    /** 每页记录数 */
    private int pageSize = 10;

    /** 查询参数 */
    private Q queryParams;

    private PageQueryBean() { }


    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public Q getQueryParams() {
        return queryParams;
    }

    public void setQueryParams(Q queryParams) {
        this.queryParams = queryParams;
    }

    public static <Q extends Serializable> PageQueryBean build(Q queryModel) {
        PageQueryBean<Q> bean = new PageQueryBean<>();
        bean.setQueryParams(queryModel);
        return bean;
    }

    public static <Q extends Serializable> PageQueryBean build(PageModel pageModel, Q queryModel) {
        PageQueryBean<Q> bean = new PageQueryBean<>();
        bean.setQueryParams(queryModel);

        if (null != pageModel) {
            bean.setPage(pageModel.getPage());
            bean.setPageSize(pageModel.getPageSize());
        }

        return bean;
    }

    public static <Q extends Serializable> PageQueryBean build(int page, int pageSize, Q queryModel) {
        PageQueryBean<Q> bean = new PageQueryBean<>();
        bean.setQueryParams(queryModel);

        if (0 < page) {
            bean.setPage(page);
        }

        if (1 <= pageSize) {
            bean.setPageSize(pageSize);
        }

        return bean;
    }

}
